/*
 * 367dns JavaScript Library
 * Copyright (c) 2014 367dns.com
 */
function auth_wx_login(){

api.showProgress({title:"提示!",text:"微信授权登录中..."});
wx.isInstalled(function(ret, err) {
    if (ret.installed) {
       wx.auth({
            apiKey: 'wxfbe07a05b8124c5c'
        }, function(ret, err) {
        if (ret.status) {
            //alert(JSON.stringify(ret));

            var code = ret.code;
            //getToken
            wx.getToken({
                 apiKey: 'wxfbe07a05b8124c5c',
                 apiSecret: 'd121eccec3bcb7555b0f83a3450f8a1b',
                 code: code
            }, function(ret, err) {
               if (ret.status) {
                //alert(JSON.stringify(ret));

                 var access_token = ret.accessToken;
                 var open_id = ret.openId;
                 check_openid(open_id,access_token,'wx_auth')
               } else {
                 api.hideProgress();
                 api.toast({msg : '授权失败，请稍后再试',location: 'middle'});


               }
            });



        } else {

                api.hideProgress();
                 if(err.code == '1'){
                   api.toast({msg : '取消授权',location: 'middle'});
                 }
                  if(err.code == '3'){
                   api.toast({msg : '未安装微信客户端',location: 'middle'});
                 }
                  if(err.code == '-1'){
                   api.toast({msg : '授权失败，请稍后再试',location: 'middle'});
                }
        }
      });

    } else {
        alert('当前设备未安装微信客户端');
    }
});

}


//检查微信用户是否绑定
function  check_openid(open_id,access_token,auth_type){

  if(auth_type == 'wx_auth'){

  wx.getUserInfo({
         accessToken: access_token,
         openId: open_id
       }, function(ret, err) {
         console_debug(ret, err);
          if (ret.status) {
            var data1 =  {"data":ret};
            api.ajax({
              url:server+'api/user.wxlogin.php',
              method : 'post',
              dataType: 'json',
              data: {body: create_post_data(data1)}
            },function(ret,err){
                 console_debug(ret, err);
                 api.hideProgress();
              if(ret){
                 if (ret.status == 1) {
                   api.setPrefs({ key : 'userinfo',value : ret.data});
                   api.sendEvent({name : 'reg_login_successEvent',extra : 'login'});
                   api.sendEvent({name : 'userinfo_update',extra : 'update'});
                   api.sendEvent({name : 'notice_update',extra : 'update'});
                   api.alert({title: '提示',msg: ret.message}, function(ret, err) {
                         var index = ret.buttonIndex;
                         api.closeWin({});
                   });
                 }else{
                   blind_more(ret.error,data1.data);
                 }
               }else{
                  api.toast({msg : '授权登录失败'});
               }
            })

       } else {
              api.hideProgress();
               //alert(err.code);
                api.toast({msg : ret.error,location: 'middle'});
       }
     });

  }

}

function blind_more(msg,wxinfo){
  openWin('wx_login',api.wgtRootDir+'/html/common_header.html',{wxinfo:wxinfo,bounces:0,frame:'login',title:'绑定手机号码'});
  return;

  api.actionSheet({
      title: msg,
      cancelTitle: '取消',
      buttons: ['绑定已注册用户', '绑定新用户']
 }, function(ret, err) {
      var index = ret.buttonIndex;
      if(index == 1){
         openWin('wx_login',api.wgtRootDir+'/html/common_header.html',{wxinfo:wxinfo,bounces:0,frame:'login',title:'绑定已注册用户'});
      }
      if(index == 2){
         openWin('wx_register',api.wgtRootDir+'/html/common_header.html',{wxinfo:wxinfo,bounces:0,frame:'register',title:'绑定新用户'});
      }

 });
}


function check_wxqq(){

    wx.isInstalled(function(ret, err) {
    if (ret.installed) {
        var  ct = $api.dom('#socil-list');
        $api.attr(ct,'style','display:block !important;');
        var wxct = $api.dom('#wx_login');
        $api.removeCls(wxct, 'aui-hide');
    }

    });

    qq.installed(function(ret, err) {
      if (ret.status) {
        var  ct = $api.dom('#socil-list');
        $api.attr(ct,'style','display:block !important;');
        var qqct = $api.dom('#qq_login');
        $api.removeCls(qqct, 'aui-hide');
      }
    });

}
